package com.autumn.util.convert;

import java.util.Date;
import com.autumn.util.DateUtils;
import com.autumn.util.StringUtils;


/**
 * 日期类型转换
 * 
 * @author 杨昌国
 *
 *         2017-10-10 11:32:43
 */
public class DateConvert extends AbstractDataConvert {

	@Override
	public final boolean isBaseType() {
		return true;
	}

	@Override
	public final boolean isNumberType() {
		return false;
	}

	@Override
	public Object getDefaultValue() {
		return null;
	}

	@Override
	public Class<?> getType() {
		return Date.class;
	}

	@Override
	protected Object convert(Class<?> targetClass, Class<?> sourceClass, Object source) {
		if (sourceClass.equals(long.class)) {
			long value = (long) source;
			return new Date(value);
		}
		if (sourceClass.equals(Long.class)) {
			Long value = (Long) source;
			return new Date(value.longValue());
		}
		if (Date.class.isAssignableFrom(sourceClass)) {
			java.sql.Date value = (java.sql.Date) source;
			return new Date(value.getTime());
		}
		if (sourceClass.equals(String.class)) {
			String value = source.toString().trim();
			if (StringUtils.isNullOrBlank(value)) {
				return this.getDefaultValue();
			}
			return DateUtils.parseDate(value);
		}
		throw this.throwConvertException(sourceClass, source);
	}

}
